package com.br.fmn.clubedoimovel.negocio.model;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;

import com.br.fmn.clubedoimovel.R;
import com.br.fmn.clubedoimovel.negocio.entidade.Coordinates;
import com.br.fmn.clubedoimovel.negocio.entidade.PlacesAutocompleteList;
import com.br.fmn.clubedoimovel.negocio.entidade.PlacesAutocompleteList.PlaceAutoComplete;
import com.br.fmn.clubedoimovel.negocio.servico.PlaceService;
import com.br.fmn.clubedoimovel.view.PrepareOverlay;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class ApplicationMapModel {

	private MapView mapView;
	private Context context;
	private PlaceService placesService;
	private PrepareOverlay itemizedOverlay;
	private List<Overlay> mapOverlays;
	private static Coordinates coordinates;

	public ApplicationMapModel(Context context, MapView mapView) {

		this.mapView = mapView;
		this.context = context;
	}

	/**
	 * Enable location.
	 */
	public void enableMyLocation() {

		MyLocationOverlay mylocationOverlay = new MyLocationOverlay(context,
				mapView);

		mylocationOverlay.enableMyLocation();

	}

	/**
	 * Enable traffic map .
	 */
	public void habilitarTerreno() {

		mapView.setTraffic(true);
		desabilitarSatellite();
	}

	/**
	 * Enable Satellite map ;
	 */
	public void habilitarSatellite() {

		mapView.setSatellite(true);
		desabilitarTerreno();
	}

	/**
	 * EnableTraffic map.
	 * 
	 */
	public void desabilitarTerreno() {

		mapView.setTraffic(false);
	}

	/**
	 * Enable Satellite map.
	 */
	public void desabilitarSatellite() {

		mapView.setSatellite(false);
	}

	/**
	 * get currentPosition
	 * 
	 * @return
	 */
	public Location retrieveLastKnownLocation() {
		LocationManager locationManager;
		locationManager = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);
		return locationManager
				.getLastKnownLocation(LocationManager.GPS_PROVIDER);

	}

	/**
	 * Retrieve pleces by reference.
	 * 
	 * @param reference
	 * @return
	 * @throws IOException
	 */
	public List<String> getPlaces(String reference) throws IOException {

		List<String> placesToAdapter = new ArrayList<String>();

		placesService = new PlaceService();

		PlacesAutocompleteList places = placesService
				.performAutoComplete(reference);

		for (PlaceAutoComplete place : places.predictions) {

			placesToAdapter.add(place.description);

		}
		return placesToAdapter;

	}

	/**
	 * Set pino in Map.
	 * 
	 * @param location
	 * @param pinoMap
	 */
	public void setPinMap(double latitude, double longitude, int pinoMap) {

		// inicia os overlays para o mapa.
		mapOverlays = mapView.getOverlays();

		Drawable pinoOrigin = context.getResources().getDrawable(pinoMap);
		itemizedOverlay = new PrepareOverlay(pinoOrigin, mapView);

		GeoPoint point = new GeoPoint((int) (latitude * 1E6),
				(int) (longitude * 1E6));

		OverlayItem overlayItem = new OverlayItem(point, "Edificio Sol e Mar)",
				"(Construtora pbt Brasil)");

		itemizedOverlay.addOverlay(overlayItem);

		mapOverlays.add(itemizedOverlay);

		final MapController mc = mapView.getController();
		mc.animateTo(point);
		mc.setZoom(16);

	}

	/**
	 * Retrieve geocode by reference.
	 * 
	 * @param reference
	 * @return
	 * @throws IOException
	 */
	public Address getGeocode(String reference) throws IOException {

		Geocoder geocoder = new Geocoder(context);

		List<Address> address = geocoder.getFromLocationName(reference, 1);
		return address.get(0);

	}

	public static Coordinates getCoordinates() {
		return coordinates;
	}

	public static void setCoordinates(Coordinates coordinates) {
		ApplicationMapModel.coordinates = coordinates;
	}

}
